package com.itcast.framdemo.websocket;

import com.itcast.framdemo.config.GetHttpSessionConfig;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/***
 * websocket实现类
 */
@ServerEndpoint(value = "/chat/{userid}")
@Component
@Slf4j
public class ChatEndpoint {
    //    保存每一个客户端 key --> userid , value ---> session对象
    private static final Map<String,Session> sessionMap = new ConcurrentHashMap<>();

    // WebSocket连接建立时被调用
    @OnOpen
    public void onOpen(Session session, EndpointConfig config, @PathParam("userid") Integer userid) {
        log.info("onOpen targetid:{}",userid);

    }
    @OnMessage
    public void onMessage(String message) throws IOException{

    }
    /***
     * 发送消息/保存离线消息
     */
    private void sendMessage(String message,String target) throws IOException{

    }
    /**
     * 断开websocket链接时，被调用
     */
    @OnClose
    public void onClose() {

    }

    @OnError
    public void onError(Session session, Throwable error) {
        System.out.println("onError:::"+error.getMessage());
    }

}
